import { RouteRecordRaw } from 'vue-router';
// import { myStore } from '@/store/index';

let firstMenu: any = null;

export function mapMenusToRoutes(userMenu: RouteRecordRaw[]): RouteRecordRaw[] {
  const routes: RouteRecordRaw[] = [];
  // 获取所有的路由
  const allRoutes: RouteRecordRaw[] = [];
  const routeFiles = require.context('../router/main', true, /\.ts/);
  routeFiles.keys().forEach((key) => {
    const file = require('../router/main' + key.split('.')[1]);
    allRoutes.push(file.default);
  });

  // 获取type为2的路由数组
  function getRoleRoute(menu: any) {
    for (let i = 0; i < menu.length; i++) {
      const e = menu[i];
      if (e.type == '2') {
        if (!firstMenu) {
          firstMenu = e;
        }
        const singleRoute: any = allRoutes.find((el) => e.path === el.path);
        if (singleRoute) routes.push(singleRoute);
      }
      if (e.children) {
        getRoleRoute(e.children);
      }
    }
  }
  getRoleRoute(userMenu);
  return routes;
}

export { firstMenu };
